package com.br.android.widingwheels.maps;

import java.text.DecimalFormat;
import java.util.Currency;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockMapActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widingwheels.PrincipalActivity;
import com.br.android.widingwheels.R;
import com.br.android.widingwheels.db.WidingWheelsCursor;
import com.br.android.widingwheels.db.WidingWheelsDAO;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapGastosActivity extends SherlockMapActivity implements LocationListener {

	private MapView mapViewGastos;
	private LocationManager lm;

	private WidingWheelsDAO dao;
	private WidingWheelsCursor cursor;

	Currency currency = Currency.getInstance(Locale.getDefault());
	private DecimalFormat format;

	@Override
	protected void onCreate(Bundle saveInstance) {
		super.onCreate(saveInstance);
		setContentView(R.layout.layout_mapa);

		format = new DecimalFormat(currency.getSymbol() + " ###,###,##0.00");

		mapViewGastos = (MapView) findViewById(R.id.mapViewGastos);
		mapViewGastos.setBuiltInZoomControls(true);

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	    String melhorProvedor = lm.getBestProvider(new Criteria(), true);
	    Location location = lm.getLastKnownLocation(melhorProvedor);
	    String latitude = String.valueOf(location.getLatitude());
        String longitude = String.valueOf(location.getLongitude());
        String paramFS = latitude + ", " + longitude;

	    //atualizando a localização
	    lm.requestLocationUpdates(melhorProvedor, 10, 1, this);

		//icone da minha localização no mapa
		MyLocationOverlay myLocation = new MyLocationOverlay(this, mapViewGastos);
		myLocation.enableMyLocation();
		myLocation.enableCompass();
		mapViewGastos.getOverlays().add(myLocation);

		GeoPoint initGeoPoint = new GeoPoint((int)(lm.getLastKnownLocation(melhorProvedor).getLatitude() * 1000000),
					(int)(lm.getLastKnownLocation(melhorProvedor).getLongitude() * 1000000));

		MapController mapController = mapViewGastos.getController();
		mapController.setZoom(14);
		mapController.animateTo(initGeoPoint);

		//adicionando os pontos dos gastos no mapa
		dao = new WidingWheelsDAO(this);
		cursor = dao.getAll();

		Drawable imagem = getResources().getDrawable(R.drawable.pin_blue);
		MeuItemizedOverlay meuItemOverlay = new MeuItemizedOverlay(imagem, this, mapViewGastos);
		List<Overlay> listOverlay = mapViewGastos.getOverlays();

		for(int i = 0; i < cursor.getCount(); i++){
			Log.i("CURSOR", "" + cursor.getLatitudeGasto() + " - " + cursor.getLongitudeGasto());
			cursor.moveToPosition(i);
			GeoPoint point = new GeoPoint((int)(cursor.getLatitudeGasto() * 1E6), (int)(cursor.getLongitudeGasto() * 1E6));
//			mapViewGastos.getController().setCenter(point);
			OverlayItem item = new OverlayItem(point, cursor.getNomeGasto(), cursor.getNomeLocalGasto() + "\n" + "Valor: " + format.format(cursor.getValorGasto()));
			meuItemOverlay.addOverlay(item);
		}
		meuItemOverlay.populateNow();
		listOverlay.add(meuItemOverlay);

	}

	@Override
	public boolean onCreatePanelMenu(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onPreparePanel(int featureId, View view, Menu menu) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onActionModeStarted(ActionMode mode) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onActionModeFinished(ActionMode mode) {
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLocationChanged(Location location) {
		GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
		MapController controller = mapViewGastos.getController();
		controller.animateTo(geoPoint);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
